package com.zlq.cn.controller.sign.ai;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.aiart.v20221229.models.ImageToImageRequest;
import com.tencentcloudapi.aiart.v20221229.models.ImageToImageResponse;
import com.tencentcloudapi.aiart.v20221229.models.TextToImageRequest;
import com.tencentcloudapi.aiart.v20221229.models.TextToImageResponse;
import com.tencentcloudapi.facefusion.v20220927.models.FuseFaceRequest;
import com.tencentcloudapi.facefusion.v20220927.models.FuseFaceResponse;
import com.tencentcloudapi.fmu.v20191213.models.StyleImageProRequest;
import com.tencentcloudapi.fmu.v20191213.models.StyleImageProResponse;
import com.tencentcloudapi.ft.v20200304.models.*;
import com.tencentcloudapi.tiia.v20190529.models.EnhanceImageRequest;
import com.tencentcloudapi.tiia.v20190529.models.EnhanceImageResponse;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.config.Config;
import com.zlq.cn.dto.param.TencentAiParam;
import com.zlq.cn.entity.FanyiUser;
import com.zlq.cn.entity.SuggestRecord;
import com.zlq.cn.entity.TencentAi;
import com.zlq.cn.service.un.FanyiUserService;
import com.zlq.cn.service.un.SuggestRecordService;
import com.zlq.cn.service.ai.AiCommonService;
import com.zlq.cn.service.ai.TencentAiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-03-05
 */
@Api(tags = "腾讯AI")
@Validated
@RestController
@RequestMapping("/tencent/ai")
public class TencentAiController {

    @Resource
    private FanyiUserService fanyiUserService;

    @Resource
    private TencentAiService tencentAiService;

    @Resource
    private AiCommonService aiCommonService;

    @Resource
    private SuggestRecordService suggestRecordService;

//    @Value("${aizh.open}")
//    private String open;

    @ApiOperation("人脸性别转换-swapGenderPic")
    @PostMapping("/swapGenderPic")
    public SignApiResult<SwapGenderPicResponse> swapGenderPic(@RequestBody SwapGenderPicRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.swapGenderPic(req));
    }

    @ApiOperation("人脸年龄变化-changeAgePic")
    @PostMapping("/changeAgePic")
    public SignApiResult<ChangeAgePicResponse> changeAgePic(@RequestBody ChangeAgePicRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.changeAgePic(req));
    }

    @ApiOperation("人脸动漫化-faceCartoonPic")
    @PostMapping("/faceCartoonPic")
    public SignApiResult<FaceCartoonPicResponse> faceCartoonPic(@RequestBody FaceCartoonPicRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.faceCartoonPic(req));
    }

    @ApiOperation("人像渐变任务-morphFace")
    @PostMapping("/morphFace")
    public SignApiResult<MorphFaceResponse> morphFace(@RequestBody MorphFaceRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.morphFace(req));
    }

    @ApiOperation("查询人像渐变任务")
    @PostMapping("/queryFaceMorphJob")
    public SignApiResult<QueryFaceMorphJobResponse> queryFaceMorphJob(@RequestBody QueryFaceMorphJobRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.queryFaceMorphJob(req));
    }

    @ApiOperation("撤销人像渐变任务")
    @PostMapping("/cancelFaceMorphJob")
    public SignApiResult<CancelFaceMorphJobResponse> cancelFaceMorphJob(@RequestBody CancelFaceMorphJobRequest req) {
        return SignApiResult.resultSuccess(tencentAiService.cancelFaceMorphJob(req));
    }

    @ApiOperation("图像清晰度增强-enhanceImage")
    @PostMapping("/enhanceImage")
    public SignApiResult<EnhanceImageResponse> enhanceImage(@RequestBody EnhanceImageRequest param) {
        return SignApiResult.resultSuccess(tencentAiService.enhanceImage(param));
    }

    @ApiOperation("图片滤镜-styleImagePro")
    @PostMapping("/style/image/pro")
    public SignApiResult<StyleImageProResponse> styleImagePro(@RequestBody StyleImageProRequest param) {
        return SignApiResult.resultSuccess(tencentAiService.styleImagePro(param));
    }

    @ApiOperation("图片人脸融合-fuseFace")
    @PostMapping("/fuse/face")
    public SignApiResult<FuseFaceResponse> fuseFace(@RequestBody FuseFaceRequest param) {
        return SignApiResult.resultSuccess(tencentAiService.fuseFace(param));
    }

    @ApiOperation("智能文生图-textToImage")
    @PostMapping("/textToImage")
    public SignApiResult<TextToImageResponse> textToImage(@RequestBody TextToImageRequest param) {
        return SignApiResult.resultSuccess(tencentAiService.textToImage(param));
    }

    @ApiOperation("智能图生图-imageToImage")
    @PostMapping("/imageToImage")
    public SignApiResult<ImageToImageResponse> imageToImage(@RequestBody ImageToImageRequest param) {
        return SignApiResult.resultSuccess(tencentAiService.imageToImage(param));
    }

    @ApiOperation("AI图片查询")
    @PostMapping("/image/page")
    public SignApiResult<Page<TencentAi>> imagePage(@RequestBody TencentAiParam param) {
        param.setProject(Config.PROJECT_TENCENT_AI);
        return SignApiResult.resultSuccess(aiCommonService.imagePage(param.getPage(), param));
    }

    @ApiOperation("删除AI图片查询")
    @PostMapping("/delete/image")
    public SignApiResult<Boolean> deleteImage(@RequestBody TencentAi param) {
        param.setProject(Config.PROJECT_TENCENT_AI);
        return SignApiResult.resultSuccess(aiCommonService.deleteImage(param));
    }

    @ApiOperation("获取服务器时间")
    @GetMapping("/sys/time")
    public SignApiResult<Long> sysTime() {
        return SignApiResult.resultSuccess(System.currentTimeMillis());
    }

    @ApiOperation("用户登录/注册")
    @PostMapping("/login")
    public SignApiResult<FanyiUser> login(@RequestBody Object o) {
        FanyiUser fanyiUser = fanyiUserService.login(Config.PROJECT_TENCENT_AI);
//        fanyiUser.setOpen(open);
        return SignApiResult.resultSuccess(fanyiUser);
    }

    @ApiOperation("反馈")
    @PostMapping("/suggest/add")
    public SignApiResult<String> suggestRecord(@RequestBody SuggestRecord suggestRecord) {
        suggestRecord.setProject(Config.PROJECT_TENCENT_AI);
        suggestRecord.setIsreply(0);
        suggestRecord.setType(SignApiBootstrap.get().getImei());
        suggestRecord.setSubmitdate(LocalDateTime.now());
        suggestRecordService.save(suggestRecord);
        return SignApiResult.resultSuccess("反馈成功");
    }
}

